package com.cqan.entity;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.cqan.entity.pk.LongEntity;


@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "TBL_ATOM_VALUE")
//默认的缓存策略.
@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class AtomValue extends LongEntity {
	
	private AtomField field;
	
	private Boolean booleanValue;
	
	private Date dateValue;
	
	private Double doubleValue;
	
	private Float floatValue;
	
	private Integer intValue;
	
	private Long longValue;
	
	private String strValue;
	
	private Atom atom;
	
	@Transient
	@SuppressWarnings("unchecked")
	public Object getFieldValue(){
		FieldType fieldType = this.field.getFieldType();
		switch (fieldType) {
		case D:
			return dateValue;
		case I:
			return intValue;
		case L:
			return longValue;
		case S:
			return strValue;
		case B:
			return booleanValue;
		case N:
			return doubleValue;
		}
		return null;
	}
	
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "fid")
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public AtomField getField() {
		return field;
	}

	public void setField(AtomField field) {
		this.field = field;
	}

	@ManyToOne
	@JoinColumn(name = "ATOMID")
	public Atom getAtom() {
		return atom;
	}

	public void setAtom(Atom atom) {
		this.atom = atom;
	}

	@Column(name="BOOLEAN_VALUE")
	public Boolean getBooleanValue() {
		return booleanValue;
	}

	@Column(name="DATE_VALUE")
	public Date getDateValue() {
		return dateValue;
	}

	@Column(name="DOUBLE_VALUE")
	public Double getDoubleValue() {
		return doubleValue;
	}

	@Column(name="FLOAT_VALUE")
	public Float getFloatValue() {
		return floatValue;
	}

	@Column(name="INT_VALUE")
	public Integer getIntValue() {
		return intValue;
	}
	
	@Column(name="LONG_VALUE")
	public Long getLongValue() {
		return longValue;
	}

	@Column(name="STR_VALUE")
	public String getStrValue() {
		return strValue;
	}

	public void setBooleanValue(Boolean booleanValue) {
		this.booleanValue = booleanValue;
	}

	public void setDateValue(Date dateValue) {
		this.dateValue = dateValue;
	}

	public void setDoubleValue(Double doubleValue) {
		this.doubleValue = doubleValue;
	}

	public void setFloatValue(Float floatValue) {
		this.floatValue = floatValue;
	}

	public void setIntValue(Integer intValue) {
		this.intValue = intValue;
	}

	public void setLongValue(Long longValue) {
		this.longValue = longValue;
	}

	public void setStrValue(String strValue) {
		this.strValue = strValue;
	}
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
